package com.Ajava1801D.反射;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2020/10/6  14:55
 * 描述:
 */
public class TestFanShe2 {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {

        Class<KKKK> kkkkClass = KKKK.class;

        Constructor<KKKK> constructor = kkkkClass.getDeclaredConstructor(String.class, int.class);
       constructor.setAccessible(true);
       //创建的对象实例
       KKKK kkkk = constructor.newInstance("222", 555);


        //Constructor<?>[] constructors = kkkkClass.getConstructors();

        //根据方法名称获取非私有的方法
        Method setName = kkkkClass.getMethod("setName",String.class);

        setName.invoke(kkkk,"99999999999999");

        System.out.println(kkkk.getName());

        //获取所有的方法包括私有的方法
        kkkkClass.getDeclaredMethods();

        Field name = kkkkClass.getDeclaredField("name");

        name.setAccessible(true);

        name.set(kkkk,"张三");

        System.out.println(kkkk.getName());


        Autowired name1 = kkkkClass.getDeclaredField("name").getAnnotation(Autowired.class);

        System.out.println(name1.required());


        Component annotation = kkkkClass.getAnnotation(Component.class);

        System.out.println(annotation.value());

    }

}
